package com.licq.ldoc.config;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.licq.ldoc.utils.ClassUtils;

/**
 * 
 * @author liujian
 *
 */
public class Configuration {
	private static transient Log logger = LogFactory.getLog(Configuration.class);

	private static CompositeConfiguration configuration;
	
	private static String[] propertyFiles = {"context.properties", "MessageResources.properties"};
	
	private static String[] xmlFiles = {};

	private static final String ENCODING = "UTF-8";
	
	static {
		loadConfigurations();
	}
	
	private static void loadConfigurations() {
		configuration = new CompositeConfiguration();
		try {
			for (String xmlFile : xmlFiles) {
				XMLConfiguration config = new XMLConfiguration(ClassUtils
						.getDefaultClassLoader().getResource(xmlFile));
				config.setEncoding(ENCODING);
				configuration.addConfiguration(config);
			}
			for (String propertyFile : propertyFiles) {
				PropertiesConfiguration config = new PropertiesConfiguration(ClassUtils
						.getDefaultClassLoader().getResource(propertyFile));
				config.setEncoding(ENCODING);
				configuration.addConfiguration(config);
			}
		} catch (ConfigurationException e) {
			String errorMsg = "load configuration file error: " + e;
			if (logger.isDebugEnabled()) {
				logger.debug(errorMsg);
			}
			throw new RuntimeException(errorMsg);
		}
	}

	public static org.apache.commons.configuration.Configuration getConfiguration() {
		return configuration;
	}
	
}
